﻿using Microsoft.EntityFrameworkCore;
using TuLing.Dto;
using TuLing.Entities;
using TuLing.IServices;

namespace TuLing.Services
{
    public class CustomerService : BaseService, ICustomerService
    {
        public CustomerService(DefaultDbContext context) : base(context) { }

        public async Task<List<CustomerDto>> Search(string keyword = "")
        {
            if (string.IsNullOrEmpty(keyword))
            {
                var listall = await _context.Customers.ToListAsync();
                return listall.Select(m => new CustomerDto
                {
                    Id = m.Id,
                    Name = m.Name,
                    Phone = m.Phone,
                    Remark = m.Remark,
                    Address = m.Address
                }).ToList();
            }
            else
            {
                var list = await _context.Customers.Where(m => m.Name.Contains(keyword)).ToListAsync();
                return list.Select(m => new CustomerDto
                {
                    Id = m.Id,
                    Name = m.Name,
                    Phone = m.Phone,
                    Remark = m.Remark,
                    Address = m.Address
                }).ToList();
            }
        }

        public async Task Save(CustomerDto input)
        {
            var has = _context.Customers.Any(m => m.Name.Equals(input.Name) && m.Id != input.Id);
            if (has)
            {
                throw new Exception("客户名称已存在，请检查");
            }

            // 更新客户信息
            if (input.Id > 0)
            {
                var existingCustomer = _context.Customers.FirstOrDefault(m => m.Id == input.Id);
                if (existingCustomer == null)
                {
                    throw new Exception("客户不存在");
                }
                existingCustomer.Name = input.Name;
                existingCustomer.Phone = input.Phone;
                existingCustomer.Remark = input.Remark;
                existingCustomer.Address = input.Address;

                _context.Customers.Update(existingCustomer);
                await _context.SaveChangesAsync();
            }
            else
            {                
                var custormer = new Customer
                {
                    Name = input.Name,
                    Phone = input.Phone,
                    Remark = input.Remark,
                    Address = input.Address
                };

                // 新增客户
                _context.Customers.Add(custormer);
                await _context.SaveChangesAsync();

                input.Id = custormer.Id;
            }
        }

        /// <summary>
        /// 获取
        /// </summary>  
        public async Task<CustomerDto?> Get(long id)
        {
            var custom = await _context.Customers.FirstOrDefaultAsync(s => s.Id == id);
            if (custom == null)
            {
                return null;
            }
            return new CustomerDto
            {
                Id = custom.Id,
                Name = custom.Name,
                Phone = custom.Phone,
                Remark = custom.Remark,
                Address = custom.Address
            };
        }

        /// <summary>
        /// 删除 
        /// </summary>  
        public async Task Delete(long id)
        {
            var customer = await _context.Customers.FirstOrDefaultAsync(s => s.Id == id);
            if (customer == null)
            {
                throw new Exception("客户不存在");
            }
            _context.Customers.Remove(customer);
            await _context.SaveChangesAsync();
        }

    }
}
